﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebClient
{
    public partial class Shoppingcart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack) {
                this.Add();
            }
        }

        

        //them mon hang vao gio hang
        private void Add()
        {
            long id = -1;
            try
            {
                id = long.Parse(Request.Params["id"]);
            }
            catch
            {
            }
            if (id == -1)
                return;
            ArrayList list = null;
            DTO.Monhang mh = new DTO.Monhang();
            DTO.Products item = null;
            WebDataContext db = new WebDataContext();
            //WebData db = new WebData();
            List<DTO.Products> lists = db.product.findProducts("", -1);
            item = lists.SingleOrDefault(x=>x.id == id);
            mh.products = item;
            mh.soluong = 1;
            if (Session["cart"] == null)
            {
                list = new ArrayList();
                list.Add(mh);
            }
            else
            {
                list = (ArrayList)Session["cart"];
                int i = this.index(list, id);
                if (i != -1)
                {
                    mh = (DTO.Monhang)list[i];
                    mh.soluong = mh.soluong + 1;
                }
                else {
                    list.Add(mh);
                }
            }
            Session["cart"] = list;            
            CreateProducts(list);
        }

        private void CreateProducts(ArrayList list){
            System.Data.DataTable tb = new System.Data.DataTable();
            tb.Columns.Add(DTO.Products.c_id, typeof(long));
            tb.Columns.Add(DTO.Products.c_productName, typeof(String));
            tb.Columns.Add(DTO.Products.c_price, typeof(Decimal));
            tb.Columns.Add("soluong",typeof(int));
            tb.Columns.Add("thanhtien", typeof(Decimal));
            Decimal sum = 0;
            foreach (DTO.Monhang mh in list) {
                System.Data.DataRow row = tb.NewRow();
                row[DTO.Products.c_id] = mh.products.id;
                row[DTO.Products.c_productName] = mh.products.productName;
                row[DTO.Products.c_price] = mh.products.price;
                row["soluong"] = mh.soluong;
                Decimal tt = mh.soluong * mh.products.price;
                row["thanhtien"] = tt;
                sum += tt;
                tb.Rows.Add(row);
            }
            lblSum.Text = "Tổng cộng " + sum.ToString();
            Session["shopping"] = tb;
            this.grdDs.DataSource = tb;
            this.grdDs.DataBind();
        }

        private int index(ArrayList list, long id)
        {
            int i = -1;
            foreach (DTO.Monhang mh in list)
            {
                i++;
                if (mh.products.id == id)
                    return i;
            }
            return -1;
        }

        protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            Delete();
        }

        private void Delete()
        {
            ArrayList list = (ArrayList)Session["cart"];
            if (list.Count == 0)
                return;
            try
            {
                int n = this.grdDs.Rows.Count;
                    if (n > 0)
                    {
                        CheckBox chk;
                        TextBox txtSl;
                        for (int i = 0; i < n; i++)
                        {
                            txtSl = (TextBox)this.grdDs.Rows[i].FindControl("sl");
                            chk = (CheckBox)this.grdDs.Rows[i].FindControl("chkDelete");
                            if (chk.Checked == true)
                                list.RemoveAt(i);
                            else
                            {
                                DTO.Monhang mh = (DTO.Monhang)list[i];
                                int sl = 0;
                                try {
                                    sl = Convert.ToInt32(txtSl.Text);
                                }
                                catch { }
                                mh.soluong = sl;
                            }
                        }
                    }            
                }            
            catch { }
            Session["cart"] = list;
            this.CreateProducts(list);
        }
    }
}